#include "stu_return.h"
#include "ui_stu_return.h"

stu_return::stu_return(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::stu_return)
{
    ui->setupUi(this);

    appDir = QCoreApplication::applicationDirPath();
}

void stu_return::getStuname(QString name)
{
    m_who = name;
}

void stu_return::on_returnBtn_clicked()
{
    QFile stufile(appDir + "/" + m_who + ".txt");
    if (stufile.exists() == false)
    {
        qDebug() << "文件不存在" << endl;
    }

    stufile.open(QIODevice::ReadOnly);

    QStringList lines;  //字符串链表

    while (stufile.atEnd() == false) //文件没结束
    {
        QString line = stufile.readLine(); // 读取每一行字符串
        lines.append(line);    //读取每一行字符串到链表中
    }
    stufile.close();

    // 查找并删除目标行
    bool found = false;
    for (int idx = 0; idx < lines.size(); ++idx)
    {
        QString data = lines[idx];
        if (lines[idx].split("+")[0] == ui->le_bookname->text() && lines[idx].split("+")[1].split("\n")[0] == ui->le_author->text())
        {
            lines.removeAt(idx);    // 删除该行
            found = true;
            break; // 只需要删除第一个匹配项
        }
    }

    if (!found) //没找到
    {
        QMessageBox::warning(this, "归还失败", "该图书未借阅");
        return;
    }
    else
    {
        handleBookInfo();
        handleStuInfo();
    }

    stufile.open(QIODevice::WriteOnly);

    QTextStream out(&stufile);
    // 设置编码为 UTF-8
    out.setCodec("UTF-8");
    for (int idx = 0; idx < lines.size(); idx++)
    {
        out << lines[idx];
    }

    stufile.close();
}

void stu_return::on_cancelBtn_clicked()
{

}

/* 处理图书信息 */
void stu_return::handleBookInfo()
{
    QFile bookfile(appDir + "/bookInfo.txt");
    if (bookfile.exists() == false)
    {
        qDebug() << "创建bookInfo文件" << endl;
    }

    bookfile.open(QIODevice::ReadOnly);

    QStringList lines;  //字符串链表

    while (bookfile.atEnd() == false) //文件没结束
    {
        QString line = bookfile.readLine(); // 读取每一行字符串
        lines.append(line);    //读取每一行字符串到链表中
    }
    bookfile.close();

    // 查找并修改目标行
    bool found = false;
    for (int i = 0; i < lines.size(); ++i)
    {
        QString data = lines[i];
        if ((lines[i].split("+")[0] == ui->le_bookname->text()) && (lines[i].split("+")[1] == ui->le_author->text()))
        {
            QString cnt = lines[i].split("+")[2].split("\n")[0];
            cnt = QString::number(cnt.toInt() + 1);

            lines[i] = lines[i].split("+")[0] + "+" + lines[i].split("+")[1] + "+" + cnt + "\n"; // 修改该行
            found = true;
            break; // 只需要修改第一个匹配项
        }
    }

    if (!found) //没找到
    {
        QMessageBox::warning(this, "归还失败", "该书未录入图书系统");
        return;
    }
    else
    {
        ui->le_bookname->clear();
        ui->le_author->clear();
        QMessageBox::warning(this, "归还成功", "该图书已归还");
    }

    bookfile.open(QIODevice::WriteOnly);

    QTextStream out(&bookfile);
    // 设置编码为 UTF-8
    out.setCodec("UTF-8");
    for (int idx = 0; idx < lines.size(); idx++)
    {
        out << lines[idx];
    }

    bookfile.close();
}

/* 处理学生信息 */
void stu_return::handleStuInfo()
{
    QFile file(appDir + "/stuInfo.txt");
    if (file.exists() == false)
    {
        qDebug() << "创建stuInfo文件" << endl;
    }

    file.open(QIODevice::ReadOnly);

    QStringList lines;  //字符串链表

    while (file.atEnd() == false) //文件没结束
    {
        QString line = file.readLine(); // 读取每一行字符串
        lines.append(line);    //读取每一行字符串到链表中
    }
    file.close();

    // 查找并修改目标行
    bool found = false;
    for (int i = 0; i < lines.size(); ++i)
    {
        QString data = lines[i];
        if (lines[i].split("+")[0] == m_who)
        {
            QString cnt = lines[i].split("+")[3].split("\n")[0];
            cnt = QString::number(cnt.toInt() - 1);
            lines[i] = lines[i].split("+")[0] + "+" + lines[i].split("+")[1] + "+" + lines[i].split("+")[2] + "+" + cnt + "\n"; // 修改该行
            found = true;
            break; // 只需要修改第一个匹配项
        }
    }

    if (!found) //没找到
    {
        QMessageBox::warning(this, "借阅失败", "该学生不存在");
        return;
    }

    file.open(QIODevice::WriteOnly);

    QTextStream out(&file);
    // 设置编码为 UTF-8
    out.setCodec("UTF-8");
    for (int idx = 0; idx < lines.size(); idx++)
    {
        out << lines[idx];
    }

    file.close();
}

stu_return::~stu_return()
{
    delete ui;
}
